package com.datoucai.service.impl;

import com.datoucai.dao.TbNotificationDao;
import com.datoucai.entity.TbNotification;
import com.datoucai.entity.TbNotificationExample;
import com.datoucai.enums.NotificationEnumType;
import com.datoucai.service.INotificationService;
import com.datoucai.service.dto.NotificationInfoDto;
import com.datoucai.service.dto.NotificationTotalInfoDto;
import com.datoucai.utils.DateUtils;
import com.datoucai.utils.NotificationExtendUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class NotificationServiceImpl implements INotificationService {

    @Autowired
    private TbNotificationDao notificationDao;
    @Override
    public List<NotificationInfoDto> getNotificationList(Long userId) {
        log.info("查询消息通知-入参:{}",userId);
        TbNotificationExample example = new TbNotificationExample();
        TbNotificationExample.Criteria criteria = example.createCriteria();
        criteria.andReceiverIdEqualTo(userId);
        List<TbNotification> tbNotifications = notificationDao.selectByExampleWithBLOBs(example);
        List<NotificationInfoDto> notificationInfoDtos = buildNotificationInfoDtoList(tbNotifications);
        log.info("查询消息通知-出参:{}",notificationInfoDtos);
        return notificationInfoDtos;
    }

    private List<NotificationInfoDto> buildNotificationInfoDtoList(List<TbNotification> tbNotifications) {
        if(CollectionUtils.isEmpty(tbNotifications)){
            return new ArrayList<>();
        }
        List<NotificationInfoDto> notificationInfoDtos = new ArrayList<>();
        for (TbNotification tbNotification : tbNotifications) {
            NotificationInfoDto notificationInfoDto = new NotificationInfoDto();
            notificationInfoDto.setId(tbNotification.getId());
            notificationInfoDto.setType(NotificationEnumType.getByCode(tbNotification.getType()));
            notificationInfoDto.setContent(tbNotification.getContent());
            notificationInfoDto.setIsRead(tbNotification.getIsRead());
            notificationInfoDto.setCreateTime(DateUtils.date2Str(tbNotification.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
            notificationInfoDto.setSenderId(tbNotification.getSenderId());
            notificationInfoDto.setSenderName(null);
            notificationInfoDto.setSenderAvatar(null);
            notificationInfoDto.setTargetId(tbNotification.getResourceId());
            notificationInfoDto.setTargetTitle(NotificationExtendUtils.getTitleOnExtend(tbNotification.getExtend()));
            notificationInfoDto.setTargetLink(NotificationExtendUtils.getLinkOnExtend(tbNotification.getExtend()));
            notificationInfoDtos.add(notificationInfoDto);

        }
        return notificationInfoDtos;
    }

    @Override
    public NotificationTotalInfoDto getTotal(Long userId) {
        log.info("查询消息通知总数-入参:{}",userId);
        TbNotificationExample example = new TbNotificationExample();
        TbNotificationExample.Criteria criteria = example.createCriteria();
        criteria.andReceiverIdEqualTo(userId);
        long total = notificationDao.countByExample(example);
        criteria.andIsReadEqualTo((byte)1);
        long readCount = notificationDao.countByExample(example);
        long unreadCount = total-readCount;
        NotificationTotalInfoDto notificationTotalInfoDto = new NotificationTotalInfoDto();
        notificationTotalInfoDto.setTotal((int) total);
        notificationTotalInfoDto.setUnreadCount((int) unreadCount);
        notificationTotalInfoDto.setReadCount((int) readCount);
        log.info("查询消息通知总数-出参:{}",notificationTotalInfoDto);
        return notificationTotalInfoDto;
    }

    @Override
    public int markReadById(Long id) {
        log.info("标记消息已读-入参:{}",id);
        TbNotification notification = new TbNotification();
        notification.setId(id);
        notification.setIsRead((byte)1);
        int count = notificationDao.updateByPrimaryKeySelective(notification);
        log.info("标记消息已读-出参:{}",count);
        return count;
    }

    @Override
    public int markReadByIds(List<Long> ids) {
        log.info("批量标记消息已读-入参:{}",ids);
        TbNotificationExample example = new TbNotificationExample();
        example.createCriteria().andIdIn(ids);
        TbNotification notification = new TbNotification();
        notification.setIsRead((byte)1);
        int count = notificationDao.updateByExampleSelective(notification,example);
        log.info("批量标记消息已读-出参:{}",count);
        return count;
    }
}
